package com.vic.clustering;

import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;

/**
 * 测试集群搭建是否成功。
 * 生产者往 5673（rabbit1节点）生产消息，消费者从 5674（rabbit2节点）获取消息，如果能获取到表示集群搭建成功。
 */
public class Producer {

    // 队列名称
    private final static String EXCHANGE_NAME = "test_exchange_clustering";
    private final static String QUEUE_NAME = "test_queue_clustering";
    private static final String ROUTING_KEY = "aa";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 主机
        factory.setHost("10.61.100.78");
        factory.setPort(5673);
        // 用户名
        factory.setUsername("admin");
        // 密码
        factory.setPassword("123");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, false , null);

        // 绑定交换机与队列关系
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);

        // 消息
        String msg = "scalp";

        // 发送消息
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes(StandardCharsets.UTF_8));

        System.out.println("Send message successful");
    }

}
